<#include "/common/head.ftl">

<@showTitle title="${i18n.getMessage('user')} > ${i18n.getMessage('edit')}">
</@showTitle>

<div id="error" style="display:none"></div>

<form action="user/editUser.sgt" validator="user/validateUser.sgt" onsubmit="submitEditUserForm(this); return false;" onreset="$('error').hide()">
	<input type="hidden" name="id" value="${(model.id)!}"/>
	
	<table id="dataTable" align="center" cellspacing="5" cellpadding="0">
		<tr>
			<td class="name" style="width:200px"><@required/>${i18n.getMessage('username')}:</td>
			<td><input type="text" name="username" value="${(model.username)!}" style="width:200px"/></td>
		</tr>
		<tr>
			<td class="name" style="width:200px">${i18n.getMessage('password')}:</td>
			<td><input type="password" name="password" style="width:200px"/></td>
		</tr>
		<tr>
			<td class="name" style="width:200px"><@required/>${i18n.getMessage('firstName')}:</td>
			<td><input type="text" name="firstName" value="${(model.firstName)!}" style="width:200px"/></td>
		</tr>
		<tr>
			<td class="name" style="width:200px">${i18n.getMessage('lastName')}:</td>
			<td><input type="text" name="lastName" value="${(model.lastName)!}" style="width:200px"/></td>
		</tr>
		<tr>
			<td class="name" style="width:200px">${i18n.getMessage('email')}:</td>
			<td><input type="text" name="email" value="${(model.email)!}" style="width:200px"/></td>
		</tr>
		<tr>
			<td class="name">${i18n.getMessage('options')}:</td>
			<td><input type="checkbox" name="active" value="true" <#if model.active>checked</#if>/> ${i18n.getMessage('active')}</td>
		</tr>
		
		<#assign nuser = UserService.findById(user.id)>
		
		<#if nuser.isAuthenticated('Administrating')>
			<tr>
				<td class="name"><@required/>${i18n.getMessage('sites')}:</td>
				<td>
					<select name="sites" style="width:204px" multiple>
						<option></option>
						<#list sites as site>
							<option value="${(site.id)!}" <#if model.sites.contains(site)>selected</#if>>${(site.name)!}</option>
						</#list>
					</select>
				</td>
			</tr>
			<tr>
				<td class="name"><@required/>${i18n.getMessage('roles')}:</td>
				<td>
					<select name="roles" style="width:204px" multiple>
						<#list roles as role>
							<option value="${(role.id)!}" <#if model.roles.contains(role)>selected</#if>>${(role.name)!}</option>
						</#list>
					</select>
				</td>
			</tr>
		<#else>
			<#list nuser.sites as site>
				<input type="hidden" name="sites" value="${(site.id)!}"/>
			</#list>
			<#list nuser.roles as role>
				<input type="hidden" name="roles" value="${(role.id)!}"/>
			</#list>
		</#if>
		<tr>
			<td class="name" style="width:200px;vertical-align:top">${i18n.getMessage('comment')}:</td>
			<td><textarea name="comment" style="width:350px;height:80px;">${(user.comment)!}</textarea></td>
		</tr>
		<tr>
			<td class="name"></td>
			<td>
				<input type="submit" value="${i18n.getMessage('edit')}" class="button_style"/> 
				<input type="reset" value="${i18n.getMessage('reset')}" class="button_style"/> 
			</td>
		</tr>
	</table>
</form>

<script>
	$('error').hide();
	
	resetEditUserForm = function(type) {
		if(type == 'failure') {
			document.forms[0].password.value = '';
		}
	}
	
	submitEditUserForm = function(form) {
		var password = form.password.value;
		if(password != '') {
			form.password.value = hex_sha1(password);
		}
		submitFormValidation(form, '', 'post', resetEditUserForm);
	}
</script>